/**
 * Created by qianjy on 2016/11/29.
 */
require.config(window.jshead);
require(['jquery', 'pages', 'layer', 'navigator'], function ($, pg, layer) {
    layer.config({
        path: '/js/layer/'
    });

    $('#pictureCode').val('')
    $.ajax({
        type: "POST",
        url: "/user/security/information",
        dataType: "json",
        success: function (data) {

            if (data.security.identifyCard == null) {
                layer.alert('未绑定实名认证，即将跳转至实名认证页面');
                function jump() {
                    window.location.href = '/user/security/realname'
                }
                window.setTimeout(jump, 1500);
            } else {
                $.ajax({
                    type: "POST",
                    url: "/account/bank/getBankByUserId",
                    dataType: "json",
                    success: function (data) {

                        if(data.result=='fail'){
                            layer.alert('未绑定银行卡，即将跳转至银行卡页面');
                            function jump(){
                                window.location.href ='/user/security/realname'
                            }
                            window.setTimeout(jump,1500);
                        }
                    }
                });
            }
        }
    });

    //校验提现金额

    //图片验证码
    $('.validImg').click(function() {
        $(".validImg").attr("src", "/sys/ImageServlet?time=" + new Date());
    });

    //提现第一次点击
    //校验验证码长度
    $('#pictureCode').bind('blur',function(){
        var pictureCode= $('#pictureCode').val();
        if(pictureCode.length!=4 && pictureCode!=''){
            layer.msg('验证码错误');
        }
    })
    $('#withdrawalsBut').click(function () {
        var amount = $('#amount').val();
        var pictureCode = $('#pictureCode').val();
        var code=$('.bankCrad-code').attr('data-code');
        var usableMoney=parseFloat($('#usableMoney').text().replace(/,/ig,''));

        if(amount==''){
            layer.tips('请填写提现金额','#amount');
            return
        }
        if(amount<=0){
            layer.tips('金额必须大于0','#amount');
            return
        }
        if(amount>usableMoney){
            layer.tips('提现金额超过可用余额','#amount');
            return
        }
        if(pictureCode==''){
            layer.tips('请输入图形验证码','#pictureCode');
            return
        }
        var pictureCode = $('#pictureCode').val();
        $.ajax({
            type:'post',
            url:'/account/recharge/pictureCodeCheck',
            dataType:'json',
            data:{
                pictureCode:pictureCode
            },
            success:function(data){
                $('.validImg').trigger('click');
                if(data.result=='fail'){
                    layer.msg('图形验证码错误');
                }else{
                    $('.rebox').show(100);
                    $("#payPassword_rsainput").focus();
                    $('#payPassword_rsainput').val('').focus();
                    $(".guangbiao").css({"left":0});
                    $(".sixDigitPassword").find("i").find("b").css({"display":"none"});
                }
            }
        })


    });
    //取消
    $('#cancel').bind('click',function(){
        $('.rebox').hide(100);
        $('#payPassword_rsainput').val('').focus();
        $(".guangbiao").css({"left":0});
        $(".sixDigitPassword").find("i").find("b").css({"display":"none"});
    });



    //给t+1那块绑定事件
   /* $('.dayChange').bind('click',function(){
        $('#amount').val('')
     $('.interest2').html(0)
        var usableMoney=parseFloat($('#usableMoney').text().replace(/,/ig,''));
        $('.allMoney').click(function(){
            $('#amount').val(usableMoney)
            $('.balanceMoney').html('0')
            $('.interest2').html(usableMoney-5)
        })
        if($('.dayChange option:selected').val()==2){
           $('.interest1').html('5')
            $('.balanceMoney').html(usableMoney)
            $('#amount').keyup(function(){
                var amount = $('#amount').val();

                if(!reg.test(amount)){
                    layer.tips('请输入合法的金额','#amount');
                    $('#amount').val('');
                    $('.balanceMoney').html(usableMoney);
                    $('.interest2').html(0)
                }else if(amount>usableMoney){
                    layer.tips('提现金额超过可用余额','#amount');
                    $('#amount').val(usableMoney);
                    $('.balanceMoney').html(usableMoney-usableMoney);
                    $('.interest2').html(usableMoney-5)
                }else{
                    if(amount){
                        $('.balanceMoney').html(usableMoney-amount);
                        $('.interest2').html((amount-5).toFixed(2))
                    }else{
                        $('.interest2').html(0)
                    }
                }

            })
        }else if($('.dayChange option:selected').val()==1){
            var usableMoney=parseFloat($('#usableMoney').text().replace(/,/ig,''));
            $('.interest1').html('0')
            $('.allMoney').click(function(){
                $('#amount').val(usableMoney)
                $('.balanceMoney').html('0')
                $('.interest2').html(usableMoney)
            })

            $('.balanceMoney').html(usableMoney)
            $('#amount').keyup(function(){
                var amount = $('#amount').val();
                var reg = /^[1-9]{1}[0-9]{0,10}[.]{0,1}[0-9]{0,2}$/
                if(!reg.test(amount)){
                    layer.tips('请输入合法的金额','#amount');
                    $('#amount').val('');
                    $('.balanceMoney').html(usableMoney);
                    $('.interest2').html(0)
                }else if(amount>usableMoney){
                    layer.tips('提现金额超过可用余额','#amount');
                    $('#amount').val(usableMoney);
                    $('.balanceMoney').html(usableMoney-usableMoney);
                    $('.interest2').html(usableMoney)
                }else{
                    if(amount){
                        $('.balanceMoney').html(usableMoney-amount);
                        $('.interest2').html(Number(amount).toFixed(2))
                    }else{
                        $('.interest2').html(0)
                    }
                }

            })
        }
    })*/
    //点击全部提现
    $('.allMoney ').bind('click',function(){
        var usableMoney=parseFloat($('#usableMoney').text().replace(/,/ig,''));
        $('#amount').val(usableMoney)
        var amount = $('#amount').val();
        var usableMoney=parseFloat($('#usableMoney').text().replace(/,/ig,''));
        var dayChange = $('.dayChange option:selected').val()
        var reg = /^[1-9]{1}[0-9]{0,10}[.]{0,1}[0-9]{0,2}$/
        if(!reg.test(amount)){
            layer.tips('请输入合法的金额','#amount');
        }else if(amount>usableMoney){
            layer.tips('提现金额超过可用余额','#amount');
        }else {
            $.ajax({
                type: 'post',
                url: '/account/cash/countCashFee',
                data: {
                    amount: amount,
                    type: dayChange
                },
                dataType: 'json',
                success: function (data) {
                    $('.interest1').html(data.cashFee);
                    $('.interest2').html(data.actualArrival);
                    if(data.cashCharge.freeType==1){
                        $('.interest3').html("无限制");
                        $('.interest4').hide();
                    }else{
                        $('.interest4').show();
                        $('.interest3').html(data.cashCharge.free);
                        if(data.cashCharge.freeType==2){
                            if(data.cashCharge.freeTime==1){
                                $('.interest4').html("次/月");
                            }else if(data.cashCharge.freeTime==2){
                                $('.interest4').html("次/季");
                            }else if(data.cashCharge.freeTime==3){
                                $('.interest4').html("次/年");
                            }
                        }else if(data.cashCharge.freeType==3){
                            if(data.cashCharge.freeTime==1){
                                $('.interest4').html("元/月");
                            }else if(data.cashCharge.freeTime==2){
                                $('.interest4').html("元/季");
                            }else if(data.cashCharge.freeTime==3){
                                $('.interest4').html("元/年");
                            }
                        }
                    }
                    $('.balanceMoney').html(usableMoney-data.cashFee-data.actualArrival)
                }
            })
        }


    })
    $('#amount,.dayChange').bind('change',function(){
        var amount = $('#amount').val();
        var usableMoney=parseFloat($('#usableMoney').text().replace(/,/ig,''));
        var dayChange = $('.dayChange option:selected').val();
        var reg = /^[1-9]{1}[0-9]{0,10}[.]{0,1}[0-9]{0,2}$/;
        if(amount!=''){
        if(!reg.test(amount)){
            layer.tips('请输入正确的金额','#amount');
            $('.interest1').html(0);
            $('.interest2').html(0);
            $('.interest3').html(0);
            $('.interest4').html("次");
            $('.balanceMoney').html($("#usableMoney").html());
        }else if(amount>usableMoney){
            layer.tips('提现金额超过可用余额','#amount');
            $('.interest1').html(0);
            $('.interest2').html(0);
            $('.interest3').html(0);
            $('.interest4').html("次");
            $('.balanceMoney').html($("#usableMoney").html());
        }else {
            $.ajax({
                type: 'post',
                url: '/account/cash/countCashFee',
                data: {
                    amount: amount,
                    type: dayChange
                },
                dataType: 'json',
                success: function (data) {
                    $('.interest1').html(data.cashFee);
                    $('.interest2').html(data.actualArrival);
                    if(data.cashCharge.freeType==1){
                        $('.interest3').html("无限制");
                        $('.interest4').hide();
                    }else{
                        $('.interest4').show();
                        $('.interest3').html(data.cashCharge.free);
                        if(data.cashCharge.freeType==2){
                            if(data.cashCharge.freeTime==1){
                                $('.interest4').html("次/月");
                            }else if(data.cashCharge.freeTime==2){
                                $('.interest4').html("次/季");
                            }else if(data.cashCharge.freeTime==3){
                                $('.interest4').html("次/年");
                            }
                        }else if(data.cashCharge.freeType==3){
                            if(data.cashCharge.freeTime==1){
                                $('.interest4').html("元/月");
                            }else if(data.cashCharge.freeTime==2){
                                $('.interest4').html("元/季");
                            }else if(data.cashCharge.freeTime==3){
                                $('.interest4').html("元/年");
                            }
                        }
                    }
                    $('.balanceMoney').html((usableMoney-data.cashFee-data.actualArrival).toFixed(2))
                    if($('.dayChange option:selected').val()==1){
                        var test  = '此次手续费'+ data.cashFee+'元。'
                        $('.tixianshouxufeiyong1').text(test)
                    }else{
                        var test  = '此次手续费'+ data.cashFee+'元，现阶段由平台承担。'
                        $('.tixianshouxufeiyong1').text(test)
                    }
                }
            })
        }
        }else{
            $('.interest1').html(0);
            $('.interest2').html(0);
            $('.interest3').html(0);
            $('.interest4').html("次");
            $('.balanceMoney').html($("#usableMoney").html());
        }

    })
    $('.dayChange').trigger('click')
    $('.balanceMoney').text(parseFloat($('#usableMoney').text().replace(/,/ig,'')))
    //提现第二次点击
    $('#determine').bind('click', function () {

        var amount = $('#amount').val();
        var payPassword = $('#payPassword_rsainput').val();
        if(payPassword==''){
            layer.tips('请输入支付密码','#payPassword_rsainput');
            return
        }
        var index = layer.load(1, {
            shade: [0.1,'#fff'] //0.1透明度的白色背景
        });
        var type = $('.dayChange option:selected').val()
        $.ajax({
            type: "POST",
            url: "/account/cash/doCash",
            dataType: "json",
            data: {
                amount: amount,
                payPassword:payPassword,
                type:type,
            },
            success: function (data) {
                if(data.result=='success'){
                    layer.msg("申请成功");
                    setTimeout(function(){
                        window.location.href="/user/assets";
                    },1000)
                }else{
                    layer.msg(data.messageText);
                    $('#payPassword_rsainput').val('').focus();
                    $(".guangbiao").css({"left":0});
                    $(".sixDigitPassword").find("i").find("b").css({"display":"none"});
                    layer.close(index);
                }
            },
            error:function(e){
                alert(111);
            }

        });
    })
    //支付弹框
    $("#payPassword_rsainput").bind("blur", function (){
        $(".sixDigitPassword span").removeClass("guangbiao");
    });
    $("#payPassword_rsainput").bind("focus", function (){
        $(".sixDigitPassword span").addClass("guangbiao");
        if($(".i-text").val().length==6){
            $(".guangbiao").css({"left":($(".i-text").val().length-1)*50});
        }else{
            $(".guangbiao").css({"left":($(".i-text").val().length)*50});
        }
    });
    $(".i-text").keyup(function()
    {
        var inp_v = $(this).val();
        var inp_l = inp_v.length;
        //$("p").html( "input的值为：" + inp_v +"; " + "值的长度为:" + inp_l);//测试用

        for( var x = 0; x<=6; x++)
        {
            //$("p").html( inp_l );//测试
            $(".sixDigitPassword").find("i").eq( inp_l ).prevAll("i").find("b").css({"display":"block"});
            $(".sixDigitPassword").find("i").eq( inp_l - 1 ).nextAll("i").find("b").css({"display":"none"});
            $(".guangbiao").css({"left":inp_l * 50});//光标位置
            if( inp_l == 0)
            {
                $(".sixDigitPassword").find("b").css({"display":"none"});
                $(".guangbiao").css({"left":0});
            }
            else if( inp_l == 6)
            {
                $(".sixDigitPassword").find("b").css({"display":"block"});
                $(".guangbiao").css({"left":5 * 50});
            }
        }
    });
    //支付页回车事件
    $(".rebox").keydown(function() {
        if (event.keyCode == "13") {//keyCode=13是回车键
            $('#determine').click();
        }
    });
    //利息管理费鼠标滑过交互效果
    $('.hoverLixiGuanLiFei').bind('mouseover',function(){
        $('.tishiInfo').show();
    })
    $('.hoverLixiGuanLiFei').bind('mouseout',function(){
        $('.tishiInfo').hide();
    })

    $('.tiixanshouxuefei').bind('mouseover',function(){
        $('.tixianshouxufeiimg').show()
    })
    $('.tiixanshouxuefei').bind('mouseout',function(){
        $('.tixianshouxufeiimg').hide()
    })
});